<?php
/**
 * Plugin to set controller dirs to get rid of manual declaration. Called before Zend_Controller_Front 
 * begins evaluating the request against its routes. Plugin detects modules in app/controlles/ folder
 * and sets/adds controller dirs. To describe and set routes use ini files config/routes dir. Filename 
 * of config file is MODULE_NAME.ini. About ini route config read here  
 * http://framework.zend.com/manual/ru/zend.controller.router.html#zend.controller.router.routes.regex
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @category VaselinEngine
 * @package Bel Classes
 * @license  New BSD License
 *
 */
class Bel_Router_Plugin extends Zend_Controller_Plugin_Abstract {
   /**
    * Called before Zend_Controller_Front begins evaluating the
    * request against its routes.
    *
    * @param Zend_Controller_Request_Abstract $request
    * @return void
    */
    public function routeStartup(Zend_Controller_Request_Abstract $request){
    	$controller = Zend_Controller_Front::getInstance ();
    	$path_to_controllers = HOME_DIR.'app/controllers/';
    	
    	$controller->setControllerDirectory($path_to_controllers.'default', 'default');
    	$d = dir($path_to_controllers);		
		$router = $controller->getRouter();
		
        $controller->addControllerDirectory($path_to_controllers.'content','content');
       	if(file_exists(HOME_DIR.'config/routes/content.ini')){     		   		
			$config = new Zend_Config_Ini(HOME_DIR.'config/routes/content.ini', 'production');
			$router->addConfig($config, 'routes');
   		}
    				
    	while (false !== ($entry = $d->read())) {
   			if($entry!=='default' && $entry!=='..' && $entry!=='.' && $entry!=='.svn' && $entry!=='content'){
   				$controller->addControllerDirectory($path_to_controllers.$entry,$entry);
   				$ini_file_name = HOME_DIR.'config/routes/'.$entry.'.ini';   				
		
		   		if(file_exists($ini_file_name)){ 
					$config = new Zend_Config_Ini($ini_file_name, 'production');
					$router->addConfig($config, 'routes');
		   		}
   			}
		}	
		$d->close();
    }
}